package com.atguigu.day03.transform;

import com.atguigu.bean.WaterSensor;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Flink08_TransForm_Process {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //2.从端口获取数据
        DataStreamSource<String> streamSource = env.socketTextStream("localhost", 9999);

        //3.使用processs将数据转为JavaBean
        SingleOutputStreamOperator<WaterSensor> flatMap = streamSource.process(new ProcessFunction<String, WaterSensor>() {
            @Override
            public void processElement(String value, Context ctx, Collector<WaterSensor> out) throws Exception {
                String[] split = value.split(",");
                out.collect(new WaterSensor(split[0], Long.parseLong(split[1]), Integer.parseInt(split[2])));
            }
        });


        KeyedStream<WaterSensor, Tuple> keyedStream = flatMap.keyBy("id");


        //TODO 4.使用process实现累加的功能
        keyedStream.process(new KeyedProcessFunction<Tuple, WaterSensor, Tuple2<String, Integer>>() {
//           private HashMap<String, Integer> countMap = new HashMap<>();

            private ValueState<Integer> valueState;

            @Override
            public void open(Configuration parameters) throws Exception {
                valueState = getRuntimeContext().getState(new ValueStateDescriptor<Integer>("count", Integer.class,0));
            }

            @Override
            public void processElement(WaterSensor value, Context ctx, Collector<Tuple2<String, Integer>> out) throws Exception {

                Integer lastSum = valueState.value();

                int nowSum = lastSum + value.getVc();

                valueState.update(nowSum);

//                //1.判断key是否存在，如果存在的话则更新value，如果不存在的话则将自己存进去
//                if(countMap.containsKey(value.getId())){
//                    countMap.put(value.getId(), countMap.get(value.getId()) + value.getVc());
//                }else {
//                    countMap.put(value.getId(), value.getVc());
//                }
//
//                out.collect(Tuple2.of(value.getId(), countMap.get(value.getId())));

                out.collect(Tuple2.of(value.getId(),nowSum));
            }
        }).print();


        env.execute();
    }
}
